package com.linchaoen.elema.controller;

import com.linchaoen.elema.model.entity.Cart;
import com.linchaoen.elema.model.request.CartRequest;
import com.linchaoen.elema.service.CartService;
import com.linchaoen.elema.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/v1/pri/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 查询用户的所有购物车
     *
     * @param request
     * @return
     */
    @GetMapping("get_all_cart")
    public JsonData getAllCart(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("uid");
        List<Cart> cartList = cartService.findCartByUserId(userId);
        return cartList == null ? JsonData.buildError("无购物车") : JsonData.buildSuccess(cartList);
    }


    /**
     * 单次插入购物车
     *
     * @param request
     * @param foodId
     * @param num
     * @return
     */
    @GetMapping("add_cart")
    public JsonData addCart(HttpServletRequest request,
                            @RequestParam(value = "food_id") Integer foodId,
                            @RequestParam(value = "num") Integer num) {
        Integer userId = (Integer) request.getAttribute("uid");
        int rows = cartService.addCart(userId, foodId, num);
        return rows == 1 ? JsonData.buildSuccess("加购成功") : JsonData.buildError("加购失败");
    }

    /**
     * 批量插入购物车
     *
     * @param request
     * @param cartRequest
     * @return
     */
    @GetMapping("add_cart_batch")
    public JsonData addCartBatch(HttpServletRequest request,
                                 @RequestBody CartRequest cartRequest) {

        Integer uid = (Integer) request.getAttribute("uid");

        List<Cart> cartList = new ArrayList<>();
        List<Integer> idList = cartRequest.getIdList();
        List<Integer> numList = cartRequest.getNumList();

        for (int i = 0; i < idList.size(); i++) {
            for (int j = 0; j < numList.size(); j++) {
                Cart cart = new Cart();
                cart.setFoodId(idList.get(i));
                cart.setNum(numList.get(i));
                cartList.add(cart);
                break;
            }
        }
        System.out.println(cartList);
        for (Cart cart : cartList) {
            System.out.println("food_id:" + cart.getFoodId() + "  num:" + cart.getNum());
        }
//        cartService.addCartBatch(uid, cartList);
        return JsonData.buildSuccess();
    }

    @GetMapping("remove_cart")
    public JsonData removeCartByUserIdAndId(HttpServletRequest request, @RequestParam(value = "id") Integer id) {
        Integer uid = (Integer) request.getAttribute("uid");
        int rows = cartService.removeCart(uid, id);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildError("删除失败");
    }

    //更改购物车的数量
    @GetMapping("change_cart_num")
    public JsonData changeCartNum(HttpServletRequest request,
                                  @RequestParam(value = "id") Integer id,
                                  @RequestParam(value = "num") Integer num) {

        Integer uid = (Integer) request.getAttribute("uid");

        int rows = cartService.changeNum(uid, id, num);
        return rows != 0 ? JsonData.buildSuccess() : JsonData.buildError("无该数据");

    }

}
